/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2025. All rights reserved.
 * Description: Hmfs-tools resize load check point
 */

#ifndef LOAD_CHECK_POINT_H
#define LOAD_CHECK_POINT_H

#include <iostream>

#include "hmfs_fs.h"
#include "hmfs_log.h"
#include "resize_data.h"
#include "resize_utils.h"

namespace OHOS {
namespace Hmfs {
class ResizeLoad;
class LoadCheckPoint {
public:
    LoadCheckPoint(ResizeLoad *loadPtr);
    ~LoadCheckPoint() = default;
    int32_t VerifyCheckPointChecksum(CheckPointData *checkPointData);
    std::unique_ptr<uint8_t[]> GetCheckpoint(uint32_t cpStartBlkId);
    std::unique_ptr<uint8_t[]> ValidateCheckpoint(uint32_t cpStartBlkId, unsigned long long *version);
    int32_t GetAvailCheckpoint();
    int32_t HmfsCheckCheckPoint();
    int32_t HmfsLoadCpData();

private:
    bool ValidateLayout(SuperBlockData *superBlockData, CheckPointData *checkPointData, uint32_t fsMeta);
    bool ValidateUserBlockCount(SuperBlockData *superBlockData, CheckPointData *checkPointData);
    bool ValidateSegmentOffsets(CheckPointData *checkPointData, SuperBlockData *superBlockData);
    bool ValidateBitmapSizes(CheckPointData *checkPointData, SuperBlockData *superBlockData);
    int32_t ValidateCpStartSum(SuperBlockData *superBlockData, CheckPointData *checkPointData);
    bool AllocateCheckpointData();
    bool DetermineCurrentCheckpoint(std::unique_ptr<uint8_t[]> &currentCheckPoint, unsigned long long &version);
    bool CopyCheckpointData(uint8_t *currentCheckPoint);
    ResizeLoad *load_ = nullptr;
    std::shared_ptr<HmfsConfigData> config_ = nullptr;
    std::shared_ptr<HmfsSuperBlockInfo> superBlockInfo_ = nullptr;
};
} // namespace Hmfs
} // namespace OHOS
#endif // LOAD_CHECK_POINT_H
